﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Base.Objects;
using ERP_RORZE_PERMISSION.UserControls;

namespace ERP_RORZE_PERMISSION.Forms
{
    public partial class frm_UserGroupEdit : DevExpress.XtraEditors.XtraForm
    {
        public string GroupName;
        public int GroupID;
        private LogUtils log;
        private ucUserGroup parent;
        private DataTable dtStaff;

        public frm_UserGroupEdit(ucUserGroup fUser)
        {
            InitializeComponent();
            parent = fUser;
            log = new LogUtils();
            this.CancelButton = this.btCancel;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            UserGroup user = new UserGroup();
            if (GetData(user))
            {
                string update = "UPDATE [222_UserGroup] SET [GroupName] = N'" + user.GroupName +"'"+
                                " WHERE [GroupID]= " + GroupID;

                if (BaseDAO.ExecuteNonQuery(update) > 0)
                {

                    //write Log
                    log.Table = LogConstant.Table.SystemAdmin.UserGroups;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "GroupName: " + user.GroupName + "; GroupID: " + GroupID;
                    log.WriteLog();

                    if (!Utilities.MessageConfirmationYesNo(PermissionConstants.Message.UpdateSuccessAndQuestion))
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(PermissionConstants.Message.UpdateFailed);
                }
                parent.BindDataGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_UserEdit_Load(object sender, EventArgs e)
        {
            BindData();

        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        #region Private methods


        private bool GetData(UserGroup o)
        {
            #region Check valid
            if (Utilities.IsNullOrEmpty(txtGroupName.Text.Trim()))
            {
                MessageBox.Show("Group Name can not be empty.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //if (CheckDuplicate(txtUserName.Text.Trim()))
            //{
            //    MessageBox.Show("Username already exists.", PermissionConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            #endregion

            o.GroupName = txtGroupName.Text;

            return true;
        }
        
        private void BindData()
        {
            txtGroupName.Text = GroupName;
        }

        #endregion


    }
}